package org.bigk.invoices.actions.purchasers;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Namespaces;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.apache.struts2.interceptor.validation.SkipValidation;
import org.bigk.invoices.actions.AbstractCRUDAction;
import org.bigk.invoices.exceptions.ServiceException;
import org.bigk.invoices.model.Purchaser;
import org.bigk.invoices.model.PurchaserFilter;
import org.bigk.invoices.services.PurchasersService;
import org.bigk.invoices.utils.DisplayTagUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;


@Controller
@Namespaces({@Namespace("/purchasers"), @Namespace("/choosers/purchasers")})
@Results({
	@Result(name = "input", location = "/pages/purchasers/input.jsp")
})
public class PurchasersAction extends AbstractCRUDAction<Purchaser> {

	private static final long serialVersionUID = 3190832719110023997L;
	private static final Logger logger = LoggerFactory.getLogger(PurchasersAction.class);
	
	private Long id;
	private PurchaserFilter filter;
	
	@Autowired
	private PurchasersService purchasersService;

	@Override
	public void prepareList() throws ServiceException {
		logger.debug("prepareList() - started, this.filter=[{}]");
		
		if (this.filter == null) {
			this.filter = new PurchaserFilter();
		}
		this.filter.setCurrentPage(DisplayTagUtils.getCurrentPage4Id("rs"));
		
		list = purchasersService.listItems4Page(this.filter);

		logger.debug("prepareList() - finished, this.filter=[{}]");
	}

	@Actions({
		@Action(value = "list",
				results = {
						@Result(name = LIST, location = "/pages/purchasers/list.jsp")}),
		@Action(value = "chooserList",
				results = {
						@Result(name = LIST, location = "/pages/purchasers/chooserList.jsp")})
	})
	@Override
	public String list() throws ServiceException {
		return super.list();
	}
	
	@Override
	public void prepareShow() throws ServiceException {
		model = purchasersService.getPurchaser(this.getId());
	}

	@Action(value = "show",
			results = {
					@Result(location = "/pages/purchasers/show.jsp"),
					@Result(name = REDIRECT_LIST, type = "redirectAction", location = "list")
			}
	)
	@Override
	public String show() throws ServiceException {
		return super.show();
	}

	@Action(value = "create",
			results = {
					@Result(name = INPUT, location = "/pages/purchasers/input.jsp")
			}
	)
	@Override
	public String create() throws ServiceException {
		return super.create();
	}
	
	@Override
	public void prepareEdit() throws ServiceException {
		model = purchasersService.getPurchaser(this.getId());
	}
	
	@Action(value = "edit",
			results = {
					@Result(name = INPUT, location = "/pages/purchasers/input.jsp")
			}
	)	
	@Override
	public String edit() throws ServiceException {
		return super.edit();
	}

	@Override
	public void prepareSave() throws ServiceException {
		model = new Purchaser();
	}

	@Action(value = "save",
			results = {
					@Result(name = REDIRECT_LIST, type = "redirectAction", location = "list")
			}
	)
	@Override
	public String save() throws ServiceException {
		logger.debug("save() - start");
		
		purchasersService.savePurchaser(model);
		addActionMessage(getText("msg.recordInsertedSuccess"));
		
		logger.debug("save() - end - return value=[{}]", REDIRECT_LIST);
		return REDIRECT_LIST;
	}
	
	@Override
	public void prepareUpdate() throws ServiceException {
		logger.debug("prepareUpdate() - start");

		model = purchasersService.getPurchaser(id);

		logger.debug("prepareUpdate() - end");
	}
	
	@Action(value = "update",
			results = {
					@Result(name = REDIRECT_LIST, type = "redirectAction", location = "list")
			}
	)
	@Override
	public String update() throws ServiceException {
		logger.debug("update() - start");

		purchasersService.updatePurchaser(model);
		addActionMessage(getText("msg.recordUpdatedSuccess"));

		logger.debug("update() - end - return value=[{}]", REDIRECT_LIST);
		return REDIRECT_LIST;
	}

	@Override
	public void prepareDelete() throws ServiceException {
		model = purchasersService.getPurchaser(id);
	}
	
	@Action(value = "delete",
			results = {
					@Result(name = REDIRECT_LIST, type = "redirectAction", location = "list")
			}
	)
	@Override
	public String delete() throws ServiceException {
		purchasersService.deletePurchaser(model);
		addActionMessage(getText("msg.recordDeletedSuccess"));
		return REDIRECT_LIST;
	}
	
	public void prepareBackToList() throws ServiceException {
		logger.debug("prepareBackToList() - start");
		this.model = new Purchaser();
		logger.debug("prepareBackToList() - end");
	}
	
	@Action(value = "backToList",
			results = {
				@Result(name = REDIRECT_LIST, type = "redirectAction", location = "list")
			}
	)
	@SkipValidation
	public String backToList() throws ServiceException {
		logger.debug("backToList() - start - end - return value=[{}]", REDIRECT_LIST);
		return REDIRECT_LIST;
	}

	public PurchaserFilter getFilter() {
		return filter;
	}

	public void setFilter(PurchaserFilter filter) {
		this.filter = filter;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}
}
